<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hocvien extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'qlsv_model', 'hocvien');
		$this->load->module_language('member', 'qlsv');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->load->library('basic_library');
		$this->access->restrict();
	}
	
 	//ADDED BY MANHVT
 	/*
 	 * Support View thong tin hoc vien
 	 */
	public function doingalldev()
	{
		// Lấy tất cả học viên
		$this->load->helper('string');
		$allhocvien = $this->hocvien->get_all_hocvien_unlimit();
		foreach($allhocvien as $hocvien)
		{
			$arrayhv = explode(' ',$hocvien['tenhv']);
			$firstname = $arrayhv[count($arrayhv)-1];
			$lastname = $arrayhv[0];
			for($j=1;$j< count($arrayhv)-1;$j++)
			{
				$lastname = $lastname. ' '.$arrayhv[$j];
			}
			$hocvien['firstname'] = trim($firstname);
			$hocvien['lastname'] = trim($lastname);
			$hocvien['sort'] = endcodevntoasc($hocvien['firstname'].' '.$hocvien['lastname']);
			echo $hocvien['firstname'].' '.$hocvien['lastname']. '-'.$hocvien['sort'].'<br/>';
			$this->hocvien->edit_obj($hocvien);
		}
		
	}
	
	
 	
 	public function view($id)
 	{
 		//$this->access->restrict('Member.Hocvien.View');
 		//metadata
 		$data['ItemID']		 = 'User';
 		$data['title']		 = "Xem thông tin Học viên";
 		$data['description'] = "Xem thông tin Học viên";
 		$data['info'] 	= $this->hocvien->get_hocvien_info($id);
 		$data['lops'] = $this->hocvien->get_tenlop();
 		
 		$data['tinh'] = $this->hocvien->get_tinh();
 		$this->_template['page']	= 'users/hocvien/view';
 		$this->load->view('templates/default/layout/pages/users/hocvien/view', $data);
 	}
 	
 	/*
 	 * Support upload photo to hoc vien
 	 */
	public function ajaxdeletephoto($filename)
	{
		$path = 'uploads/hocvien/' . $filename;
		$this->load->helper('file');
		@unlink($path);
		
			echo json_encode(array('status' => 'successful', 'msg' => 'deleted'));
		
		
	}
	public function ajaxupload()
	{
		$status = "";
		$msg = "";
		$file_element_name = 'userfile';
		
				
		if ($status != "error")

		{
			$this->load->library('upload');
			$this->upload->initialize(
					array(
							"upload_path"   => "./uploads/hocvien",
							'allowed_types' => 'gif|jpg|png|jpeg',
							'max_size'	=> '500',
							'max_width'  => '150',
							'max_height'  => '200',
							'encrypt_name' => true,
					));

		
		
			if (!$this->upload->do_upload($file_element_name))
			{
				$status = 'error';
				$msg = $this->upload->display_errors('', '');
			}
			else
			{
				$data = $this->upload->data();
				//$file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']);
				/*if($file_id)
				{
					$status = "success";
					$msg = "File successfully uploaded";
				}
				else
				{
					unlink($data['full_path']);
					$status = "error";
					$msg = "Something went wrong when saving the file, please try again.";
				}
				*/
				$msg = $data['file_name'];
				$status = 1;
			}
			//@unlink($_FILES[$file_element_name]);
		}
		echo json_encode(array('status' => $status, 'msg' => $msg));
	}
	public function index() 
	{
		$this->access->restrict('Member.Hocvien.Index');
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Học viên';
		$data['description'] = 'Quản lý danh sách Học viên';
		
		$this->access->restrict();
		
		$this->load->library('pagination');
		
		$config = array();
		$config['base_url'] = base_url().'member/hocvien/index';
		$config['total_rows'] = $this->hocvien->count_all_hocvien();
		$config['per_page'] = 100;
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu tiên';
        $config['last_link'] = 'Cuối cùng';
        $config['next_link'] = 'Tiếp theo';
        $config['prev_link'] = 'Lùi lại';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['allsinhvien'] = $this->hocvien->get_all_hocvien($config['per_page'], $page);
		$data['lops'] = $this->hocvien->get_tenlop();
		$data['links'] = $this->pagination->create_links();		
		$data['classes'] = 	$this->hocvien->get_classes();
		$this->_template['page']	= 'users/hocvien/list';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function donghocphi($id)
	{
		$this->access->restrict('Member.Hocvien.Donghocphi');
		$info['hv'] = $this->hocvien->get_hv($id);
		$info['hocphi']= $this->hocvien->get_info_deposite_by_student($id);
		$data['infos'] = $info;
		$data['id']	=	$id;
		$this->load->library('form_validation');
		if($this->input->post('quanngu') == '0')
		{
			$this->form_validation->set_rules('phainop', 'Số tiền phải nộp', 'required|numeric');
			$this->form_validation->set_rules('lan1', 'Số tiền nộp', 'required|numeric|');
		}
		else
		{
			$this->form_validation->set_rules('phainop', 'Số tiền phải nộp', 'required');
		}
		$this->form_validation->set_rules('noidungthu', 'Nội dung thu', 'required|xls_clean|trim');
		$this->form_validation->set_rules('title', 'Mục nộp', 'required|xls_clean|trim');
		if ($this->form_validation->run() == TRUE)
		{
			$this->hocvien->add_hocphi();
			$lastid = $this->hocvien->getLastInserted();
			
			//$this->session->set_flashdata('message', 'Thu học phí thành công! Hệ thống sẽ đóng cửa sổ Form nhập sau 10 giây');	
			redirect('member/hocphi/view/'.$lastid);
		}
		else
		{
			
			$this->load->view('templates/default/layout/pages/users/hocphi/add', $data);
		}
		
	}
	
	public function search()
	{
		$this->load->library('basic_library');
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['description'] = 'Tìm kiếm thông tin';
		$data['search_keyword'] = $this->input->post('keyword', TRUE);
		//$data['lops'] = $this->hocvien->get_tenlop();
		if($data['search_keyword']!=='')
		{
			$data['result'] = $this->hocvien->search_data($data['search_keyword']);
			$this->_template['page']	= 'users/hocvien/result';
			$this->system_library->load($this->_template['page'], $data);
		}
		else
		{
			redirect('hocvien', 'refresh');
		}
	}

	public function add()
	{
		$this->access->restrict('Member.Hocvien.Add');
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm mới Học viên';
		$data['description'] = 'Thêm mới Học viên';

		$this->load->helper('form');
		$data['lops'] = $this->hocvien->get_tenlop();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		//here is validation form
		//$this->form_validation->set_rules('mahv', 'lang:form_mahv', 'required|max_length[50]|xss_clean|callback_check_mahv');
		$this->form_validation->set_rules('tenhv', 'lang:form_tenhv', 'required');
		$this->form_validation->set_rules('email', 'lang:form_email', 'xss_clean|trim');
		$this->form_validation->set_rules('nguyenquan','lang:form_nguyenquan','required|max_length[255]');
		$this->form_validation->set_rules('thuongtru','lang:form_thuongtru','max_length[255]');
		$this->form_validation->set_rules('didong','lang:form_didong','max_length[15]');
		$this->form_validation->set_rules('gioitinh','lang:form_gioitinh','');
		$this->form_validation->set_rules('cmnd','lang:form_cmnd','required|numeric|callback_check_cmnd');
		$this->form_validation->set_rules('ngaycap','lang:form_ngaycap','required|callback_date_check');
		$this->form_validation->set_rules('ngaysinh','lang:form_ngaysinh','required|callback_date_check');
		
		if($this->input->post('bodoi')=='1')
		{
			$this->form_validation->set_rules('sothexuatngu','lang:form_sothexuatngu','required');
			$this->form_validation->set_rules('ngayxuatngu','lang:form_ngayxuatngu','required|callback_date_check');
			$this->form_validation->set_rules('soquyetdinh','Số quyết định','required');
		}
		else
		{
			$this->form_validation->set_rules('sothexuatngu','lang:form_sothexuatngu','');
			$this->form_validation->set_rules('ngayxuatngu','lang:form_ngayxuatngu','');
		}
		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->hocvien->add_hocvien();
			$this->session->set_flashdata('message', 'Thêm mới thành công');	
			redirect('member/hocvien/', 'refresh');
		}	
		$data['tinh'] = $this->hocvien->get_tinh();
		$this->_template['page']	= 'users/hocvien/add';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	function get_huyen($tinh_id)
	{
		
      header('Content-Type: application/x-json; charset=utf-8');
      echo(json_encode($this->hocvien->get_huyens($tinh_id)));
	}
	
	function get_xa($huyen_id)
	{
		header('Content-Type: application/x-json; charset=utf-8');
      	echo(json_encode($this->hocvien->get_xas($huyen_id)));
	}
	function date_check($date) 
	{
    	$ddmmyyy='(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2}';
    	if(preg_match("/$ddmmyyy$/", $date))
    	{
      		return TRUE;
    	} else
    	{
     		$this->form_validation->set_message('date_check', 'Nhập định dạng ngày tháng dd/mm/yyyy');
     	return FALSE;
    	}
	}  
		
	public function check_mail()
	{
        if($this->hocvien->check_mail())
        {
            $this->form_validation->set_message('check_mail','Email đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
	public function check_cmnd()
	{
        if($this->hocvien->check_cmnd())
        {
            $this->form_validation->set_message('check_cmnd','CMND đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }

	//Edit product
	public function edit($id)
	{
		$this->access->restrict('Member.Hocvien.Edit');
		//metadata
		$data['ItemID']		 = 'User';
		$data['title']		 = "Sửa thông tin Học viên"; 
		$data['description'] = "Sửa thông tin Học viên"; 
		$data['info'] 	= $this->hocvien->get_hocvien_info($id);
		$data['lops'] = $this->hocvien->get_tenlop();
		$this->form_validation->set_rules('tenhv', 'lang:form_tenhv', 'required');
		$this->form_validation->set_rules('email', 'lang:form_email', 'xss_clean|trim');
		$this->form_validation->set_rules('nguyenquan','lang:form_nguyenquan','required|max_length[255]');
		$this->form_validation->set_rules('thuongtru','lang:form_thuongtru','max_length[255]');
		$this->form_validation->set_rules('didong','lang:form_didong','max_length[15]');
		$this->form_validation->set_rules('gioitinh','lang:form_gioitinh','numeric');
		$this->form_validation->set_rules('ngaysinh','lang:form_ngaysinh','required|callback_date_check');
		
		if($this->input->post('bodoi')=='1')
		{
			$this->form_validation->set_rules('sothexuatngu','lang:form_sothexuatngu','required');
			$this->form_validation->set_rules('ngayxuatngu','lang:form_ngayxuatngu','required|callback_date_check');
			$this->form_validation->set_rules('soquyetdinh','Số quyết định','required');
		}
		
		if($data['info']['cmnd'] == $this->input->post('cmnd') || $data['info']['cmnd'] == '')
		{
			$this->form_validation->set_rules('cmnd','lang:form_cmnd','required');
		}
		else
		{
			$this->form_validation->set_rules('cmnd','lang:form_cmnd','required|numeric|callback_check_cmnd');
			$this->form_validation->set_rules('ngaycap','lang:form_ngaycap','required|callback_date_check');
		}
		
		if($data['info']['email'] == $this->input->post('email'))
		{
			$this->form_validation->set_rules('email','lang:form_email','');
		}
		else
		{
			$this->form_validation->set_rules('email','lang:form_email','required|callback_check_mail');
		}
					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			$this->hocvien->edit_hocvien($id);			
			$button = $this->input->post('typebutton');
			$this->session->set_flashdata('message', 'Cập nhật thành công!');
			if($button == 1)
			{
				redirect('member/hocvien/edit/'.$id, 'refresh');
			}
			else {
				redirect('member/hocvien', 'refresh');
			}
		}	
		$data['tinh'] = $this->hocvien->get_tinh();
		$this->_template['page']	= 'users/hocvien/edit';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function update()
	{
		$this->hocvien->do_update();
	}
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->hocvien->change_status_hocvien($id,1);
		redirect('member/hocvien', 'refresh');
		
	}
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->hocvien->change_status_hocvien($id,0);
		redirect('member/hocvien', 'refresh');
	}
	
	public function exportexcel()
	{
		$hocviens = $this->hocvien->get_hocvien_by_class();
		// Tạo mảng để xuất ra file
		$excels = array();
		$header = array('STT','Họ và Tên','Ngày Sinh','Giới Tính','Địa chỉ','CMND','Điện thoại','Số thẻ xuất ngũ', 'Số quyết định', 'Ngày xuất ngũ');
		$excels[1]= $header;
		$j = 1;
		foreach ($hocviens as $item)
		{
			//var_dump($item);exit();
			$arr = array();
			$arr[] = $j;
			$arr[] = $item['tenhv'];
			$arr[] = date('d/m/Y',strtotime($item['ngaysinh']));
			$arr[] = $item['gioitinh']==1?'Nam':'Nữ';
			$arr[] = $item['nguyenquan'];
			$arr[] = $item['cmnd'];
			$arr[] = $item['didong'];
			if($item['bodoi']==1)
			{
				$arr[] = $item['sothexuatngu'];
				$arr[] = $item['soquyetdinh'];
				$arr[] = date('d/m/Y',strtotime($item['ngayxuatngu']));
			}
			else {
				$arr[] = '';
				$arr[] = '';
				$arr[] = '';
			}
			$excels[] = $arr;
			$j++;
		}
		// Load thư viện xuất file
		$this->load->library('excel');
		$this->excel->setAll('UTF-8', false, 'Danh sach hoc sinh');
		$this->excel->addArray($excels);
		$this->excel->generateXML('danh-sach-hoc-vien-t19');		
	}
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/products.php */